﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsina.Utility
{
    public enum LogType
    {
        Error,
        Debug,
        Info
    }

    public static class LogHelper
    {
        //日志保存路径，不包括文件名
        private static string LogPath = System.AppDomain.CurrentDomain.BaseDirectory + "Log";
        static void Write(LogType type, string txt)
        {
            try
            {
                string filePath = "";
                switch (type)
                {
                    case LogType.Info:
                        filePath = LogPath + "\\info\\";
                        break;
                    case LogType.Debug:
                        filePath = LogPath + "\\debug\\";
                        break;
                    case LogType.Error:
                        filePath = LogPath + "\\error\\";
                        break;
                }

                //文件不存在，则创建新文件
                if (!System.IO.Directory.Exists(filePath))
                {
                    System.IO.Directory.CreateDirectory(filePath);
                }
                string file = filePath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(file, true, System.Text.Encoding.UTF8))
                {
                    sw.WriteLine(Environment.NewLine);
                    sw.WriteLine(DateTime.Now.ToShortTimeString());
                    sw.WriteLine(txt);
                }
            }
            catch (Exception ex)
            {
            }
        }
        public static void Error(string txt)
        {
            Write(LogType.Error, txt);
        }

        public static void Debug(string txt)
        {
            Write(LogType.Debug, txt);
        }

        public static void Info(string txt)
        {
            Write(LogType.Info, txt);
        }
    }
}
